<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.js"></script>
</head>
<body>
  <script>
    new Promise((resolve, reject) => { // 执行器
      // 执行异步任务  ajax/定时器
      setTimeout(() => {
        // 将promise变为成功的, 指定成功的数据value
        // resolve(value)
        // 将promise变为失败的, 指定失败的数据reason
        // reject(reason)
      }, 1000);

      reject(1)
    }).then(
      value => {
        console.log('onResolved111', value)
      },
      reason => {
        console.log('onRejected111', reason)
        // 1. 抛出错误   => 失败, reason就是抛出的错误
        // throw 2
        // 2. 返回失败的promise => 失败, reason是失败promise的reason
        // return Promise.reject(3)
        // 3. 返回成功的promise => 成功, value是成功promise的value
        // return Promise.resolve(4)
        // 4. 返回pending的promise => 还是peding
        // return new Promise(() => {})
        // 5. 返回其它任意值 => 成功, value就是返回值
        return 5
      }
    ).then(
      value => {
        console.log('onResolved222', value)
        // throw 6
      },
      reason => {
        console.log('onRejected222', reason)
      }
    ).catch(
      reason => {
        console.log('onCatch', reason)
      }
    ).finally(() => {
      console.log('onFinally')
    })
    /* .then(
      value => value,
      reason => {
        console.log('onCatch', reason)
      }
    ) */

    /* 
    then返回的promise的结果状态由 then指定的回调函数执行的结果决定
    1. 抛出错误   => 失败, reason就是抛出的错误
    2. 返回失败的promise => 失败, reason是失败promise的reason
    3. 返回成功的promise => 成功, value是成功promise的value
    4. 返回pending的promise => 还是peding
    5. 返回其它任意值 => 成功, value就是返回值
    */


    const p1 = Promise.reject(3)
    const p2 = Promise.resolve(4)
    const p3 = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(5)
      }, 1000);
    })
    const p4 = Promise.all([p3, p1, p2])
    console.log(p4)
    const p5 = Promise.allSettled([p3, p1, p2])
    console.log(p5)
    const p6 = Promise.race([p3, p1, p2])
    console.log(p6)


    async function fn() {
      try {
        const response = await axios.get('./08_product2.json')
        return response.data
      } catch (error) {
        console.log(error)
        throw error
        // return Promise.reject(error)
      }
    }

    function test() {
      const p = fn()
    }
    test()


    /* 
    回调函数:
    1. 你定义
    2. 你不去亲自调用
    3. 在某个时刻它最终会自动执行
    */

  </script>
</body>
</html>